import Vue from 'vue'
import Router from 'vue-router'
import Vue1 from "../main"
import { Message } from 'element-ui';

Vue.use(Router)



const VueRouterPush = Router.prototype.push

Router.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login', //登录界面
      component: () => import('@/components/common/login')
    },
    {
      path: '/index', //教师主页
      component: () => import('@/components/admin/index'),
      children: [
        {
          path: '/', //首页默认路由
          component: () => import('@/components/common/hello')
        },
        {
          path: '/grade', //学生成绩
          component: () => import('@/components/charts/grade')
        },
        {
          path: '/selectExamToPart', //学生分数段
          component: () => import('@/components/teacher/selectExamToPart')
        },
        {
          path: '/scorePart',
          component: () => import('@/components/charts/scorePart')
        },
        {
          path: '/allStudentsGrade', //所有学生成绩统计
          component: () => import('@/components/teacher/allStudentsGrade')
        },
        // {
        //   path: '/examDescription', //考试管理功能描述
        //   component: () => import('@/components/teacher/examDescription')
        // },
        {
          path: '/selectExam', //查询所有考试
          component: () => import('@/components/teacher/selectExam')
        },
        {
          path: '/addExam', //添加考试
          component: () => import('@/components/teacher/addExam')
        },
        // {
        //   path: '/answerDescription', //题库管理功能介绍
        //   component: ()=> import('@/components/teacher/answerDescription')
        // },
        {
          path: '/selectAnswer', //查询所有题库
          component: () => import('@/components/teacher/selectAnswer')
        },
        {
          path: '/addAnswer', //增加题库主界面
          component: () => import('@/components/teacher/addAnswer')
        },
        {
          path: '/editAnswerChildren', //编辑题库主界面
          component: () => import('@/components/teacher/editAnswerChildren')
        },
        {
          path: '/addAnswerChildren', //点击试卷跳转到添加题库页面
          component: () => import('@/components/teacher/addAnswerChildren')
        },
        {
          path: '/studentManage', //学生管理界面
          component: () => import('@/components/teacher/studentManage')
        },
        {
          path: '/addStudent', //添加学生
          component: () => import('@/components/teacher/addStudent')
        },
        {
          path: '/teacherManage',
          component: () => import('@/components/admin/tacherManage')
        },
        {
          path: '/addTeacher',
          component: () => import('@/components/admin/addTeacher')
        },
        {
          path: '/addCollege',//添加专业
          component: () => import('@/components/admin/addCollege')
        },
        {
          path: '/collegeManage',
          component: () => import('@/components/admin/collegeManage')
        },
        {
          path: '/faceManage',
          component: () => import('@/components/admin/faceManage')
        }
      ]
    },
    {
      path: '/student',
      component: () => import('@/components/student/index'),
      children: [
        { path: "/", component: () => import('@/components/student/myExam') },
        { path: '/startExam', component: () => import('@/components/student/startExam') },
        { path: '/manager', component: () => import('@/components/student/manager') },
        { path: '/examMsg', component: () => import('@/components/student/examMsg') },
        { path: '/message', component: () => import('@/components/student/message') },
        { path: '/studentScore', component: () => import("@/components/student/answerScore") },
        { path: '/scoreTable', component: () => import("@/components/student/scoreTable") }
      ]
    },
    { path: '/answer', component: () => import('@/components/student/answer') }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path == "/answer") {
    let isFlag = true
    let time = null
    let time2 = null
    let number = 3
    let num = 0;
    window.onmouseover = () => {
      if (!isFlag) {
        isFlag = true
        if (number == 0) {
          next('/student')
        }
        clearInterval(time)
        clearInterval(time2)
        if (num != number) {
          Message.warning(`你离开页面过久,你还有${number}次机会,机会为0时,会将你退出到首页`)
        }
      }
    }
    window.onmouseout = () => {
      if (!isFlag) {
        isFlag = true
        if (number == 0) {
          next('/student')
        }
        clearInterval(time)
        clearInterval(time2)
        if (num != number) {
          Message.warning(`你离开页面过久,你还有${number}次机会,机会为0时,会将你退出到首页`)
        }

      }
    }
    window.onblur = () => {
      isFlag = false
      num = number
      time = setTimeout(() => {
        number--
      }, 3000)
      //退出时间超过30秒直接退出
      time2 = setTimeout(() => {
        isFlag = true
        next('/student')
      }, 15000)
    }
  }
  next()
});

export default router
